package 数据结构基础;

public class ArrayList<E> extends AbstractList<E>{
    private Object [] arr = new Object[10];
    private int size = 0;


    @Override
    public int size() {
        return size;
    }

    @Override
    public void add(E e, int index) {
        if (index > size) throw new IllegalArgumentException("插入的位置不合法!");
        // 如果元素数量大于数组容量，数组扩容
        if (size >= this.arr.length){
            Object [] arr1 = new Object[this.arr.length + 10];
            for (int i = 0;i<this.arr.length;i++){
                arr1[i] = this.arr[i];
            }
            this.arr = arr1;
        }
        // 插入元素

        for (int i = index;i < size;i++){
            arr[i+1] = arr[i];
        }
        arr[index] = e;
        size ++;
    }

    @Override
    public E remove(int index) {
        if (index > size)throw new IllegalArgumentException("删除的位置不合法！");
        for (int i = index;i<size;i++){
            this.arr[i] = this.arr[i+1];
        }
        size--;
        return (E)arr[index];

    }

    @Override
    public E get(int index) {
        if (index > size) throw new IllegalArgumentException("获取的位置大于元素数量!");
        E result = (E)arr[index];
        return result;
    }

}
